import {
  CreateParams,
  DeleteParam,
  DetailUserListItem,
  DetailUserListParam,
  EditParams,
  UserListItem,
  UserTableItem,
  UserTableParams,
} from './model/userModel';
import { workOrderHttp } from '/@/utils/http/axios';

enum Api {
  GET_USER_TABLE = '/v1/userManager/page',
  EDIT_USER = '/v1/userManager/update',
  CREATE_USER = '/v1/userManager/create',
  DELETE_USER = '/v1/userManager/',
  GET_USER_LIST = '/v1/userManager/userList',
  GET_DETAIL_USER_LIST = '/v1/userManager',
}

export const getUserTable = async (params: UserTableParams) => {
  const res = await workOrderHttp.post<UserTableItem>({
    url: Api.GET_USER_TABLE,
    params,
  });
  return res;
};

export const editUser = async (params: EditParams) => {
  const res = await workOrderHttp.put<string>({
    url: Api.EDIT_USER,
    params,
  });
  return res;
};

export const createUser = async (params: CreateParams) => {
  const res = await workOrderHttp.post<string>({
    url: Api.CREATE_USER,
    params,
  });
  return res;
};

export const deleteUser = async (params: DeleteParam) => {
  const res = await workOrderHttp.delete<string>({
    url: Api.DELETE_USER,
    params,
  });
  return res;
};

export const getUserList = async () => {
  const res = await workOrderHttp.get<UserListItem[]>({
    url: Api.GET_USER_LIST,
  });
  res.map((item) => {
    return {
      label: item.name,
      value: item.id,
    };
  });
  return res;
};

export const getDetailUserList = async (params: DetailUserListParam) => {
  const res = await workOrderHttp.get<DetailUserListItem[]>({
    url: Api.GET_DETAIL_USER_LIST,
    params: params.groupId + '/worksheetUserList',
  });
  res.map((item) => {
    item.key = item.id;
    return item;
  });
  return res;
};
